﻿using StardewModdingAPI;
using StardewModdingAPI.Events;
using StardewValley;

namespace WorseGame.Content
{
    public class 失眠(IMonitor 监视器, IModHelper 助手)
    {
        private readonly IMonitor 监视器 = 监视器;
        private readonly IModHelper 助手 = 助手;
        private const double 基础失眠概率 = 0.25;
        private bool 今日失眠 = false;

        public void Initialize()
        {
            助手.Events.GameLoop.DayStarted += 每日开始时;
        }
        private void 每日开始时(object? sender, DayStartedEventArgs e)
        {
            今日失眠 = false;
            决定是否失眠();
            if (今日失眠)
            {
                应用失眠效果();
            }
        }
        private void 决定是否失眠()
        {
            今日失眠 = new Random((int)Game1.uniqueIDForThisGame + (int)Game1.stats.DaysPlayed).NextDouble() < 基础失眠概率;
        }
        private static void 应用失眠效果()
        {
            Random 随机数 = new((int)Game1.uniqueIDForThisGame + (int)Game1.stats.DaysPlayed);
            float 体力惩罚比例 = 0.2f + (float)随机数.NextDouble() * 0.2f;
            int 原最大体力 = Game1.player.MaxStamina;
            int 新最大体力 = (int)(原最大体力 * (1 - 体力惩罚比例));
            Game1.player.stamina = 新最大体力;
            if (Game1.player.stamina > Game1.player.MaxStamina)
            {
                Game1.player.stamina = Game1.player.MaxStamina;
            }
            Game1.addHUDMessage(new HUDMessage("昨晚你失眠了，你的身体没有得到好的休息。", 3));
            Game1.playSound("debuffHit");
        }
    }
}